/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;


import com.sun.xml.ws.util.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.DbManager;
import utils.Prodotti;
import utils.Utilizzatore;

/**
 *
 * @author Alice
 */
public class Registrazione extends HttpServlet {
    private DbManager manager;
    
 @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (DbManager)super.getServletContext().getAttribute("dbmanager");
}

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     
 String username= request.getParameter("username");
 String nome= request.getParameter("nome");
 String cognome= request.getParameter("cognome");
 String indirizzo= request.getParameter("indirizzo");
 String password= request.getParameter("password");
 String cpassword= request.getParameter("cpassword");
 String email= request.getParameter("email");
 String cemail= request.getParameter("cemail");
 System.out.println("EmAil"+email);
 
 
 //CONTROLLO USERNAME
 /**String vuota="";
 if( vuota.equals(username)){
     String message3="Campi vuoti ! Compilarli tutti !";
     response.sendRedirect("error_registraz.jsp?message="+URLEncoder.encode(message3, "UTF-8"));
 }*/
        
 if((password.equals(cpassword))&&((email.equals(cemail)))){

   Date data=new Date();
   Timestamp time=new Timestamp(data.getTime());
        
   int count=0;
                    try {
                        count = manager.controlloUsername(username);
                    } catch (SQLException ex) {
                        Logger.getLogger(Registrazione.class.getName()).log(Level.SEVERE, null, ex);
                    }
            if(count!=0){
                String message="Username non disponibile! Inserisci un altro!";
                response.sendRedirect("error.jsp?message="+URLEncoder.encode(message,"UTF-8"));
            }
            else{    
        try {
            
            Utilizzatore u = manager.upRegistrazione(username,nome,cognome,password,email,time,indirizzo); 
            } catch (SQLException ex) {
        Logger.getLogger(Registrazione.class.getName()).log(Level.SEVERE, null, ex);
        }
     response.sendRedirect("registrato.jsp");
            }}else{
     String message2="Email o Password Non corrispondono !";
     response.sendRedirect("error.jsp?message="+URLEncoder.encode(message2, "UTF-8"));
 }
 
        
 

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
